//
//  LoanHistoryViewController.m
//  Rong360
//
//  Created by tianliwei on 9/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "LoanHistoryViewController.h"
#import "LoanHisCell.h"
#import "LoanHisUnit.h"
@interface LoanHistoryViewController ()
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *subTitles;
@end

static NSString *CellIdentifier = @"Cell";

@implementation LoanHistoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)initSegmentControl{
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"等额本息",@"等额本金",nil];
    
    //初始化UISegmentedControl
    
    self.segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    self.segmentedControl.frame = CGRectMake(10.0, 10.0, 300.0, 35.0);
    
    self.segmentedControl.selectedSegmentIndex = self.loanType;//设置默认选择项索引
    
    self.segmentedControl.tintColor = RGBCOLOR(0, 172, 238);
    
    //有基本四种样式
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
    
    [self.segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];  //添加委托方法
    [self.view addSubview:self.segmentedControl];
}



//具体委托方法实例

-(void)segmentAction:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    switch (Index) {
        case 0:
            self.loanType = BX;
            break;
        case 1:
            self.loanType = BJ;
            break;
        default:
            break;
    }
    [self.tableView reloadData];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    self.view.backgroundColor = RGBCOLOR(245, 245, 245);
    [self initSegmentControl];
    DBMan *dbman = [DBMan shareDB];
    self.totalDic = [dbman getAllDicInLoanHist];
    self.secTimeArr = [dbman getAllSectionTimeInLoanHist];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, UI_SCREEN_WIDTH, UI_NAVIFRAME_HEIGHT-50) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundView = nil;
    self.tableView.rowHeight = 55;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.backgroundColor = RGBCOLOR(245, 245, 245);
    [self.tableView registerNib:[UINib nibWithNibName:@"LoanHisCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    [self.view addSubview:self.tableView];
    self.title = @"房贷计算历史";
    self.titles = @[@""];
    self.subTitles = @[@""];
}
#pragma mark - Table view data source


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 30;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    ToolOperation* tool = [ToolOperation shareTool];
    
    UILabel* lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, 29, 305, 1)];
    line.backgroundColor = RGBCOLOR(219, 219, 219);
    [lab addSubview:line];
    lab.backgroundColor = [UIColor whiteColor];
    lab.textAlignment = NSTextAlignmentLeft;
    lab.font = [UIFont fontWithName:@"Arial-BoldMt" size:18];
    lab.textColor = [UIColor colorWithRed:101.0/255.0 green:101.0/255.0 blue:101.0/255.0 alpha:1];
    
    lab.text = [NSString stringWithFormat:@"   %@",[tool getTimeIsToday:[self.secTimeArr objectAtIndex:section]]];
    lab.shadowColor = [UIColor whiteColor];
    lab.shadowOffset = CGSizeMake(0, 1.0f);
    return lab;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"=====> SectionsNo.:%d",[self.secTimeArr count]);
    return [self.secTimeArr count];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString* timeStr = [self.secTimeArr objectAtIndex:section];
    NSMutableArray* array = [self.totalDic objectForKey:timeStr];
    
    return [array count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LoanHisCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor whiteColor];
    cell.backgroundView = [[UIView alloc]init];
    NSString* title = [self.secTimeArr objectAtIndex:indexPath.section];
    NSMutableArray* array = [self.totalDic objectForKey:title];
    LoanHisUnit * hut = [array objectAtIndex:indexPath.row];
    NSString* titStr = @"";
    if ( [hut.bloan intValue] == 0 ) {
        
        titStr = [NSString stringWithFormat:@"公积金%@万",hut.gloan];
        
    }else if( [hut.gloan intValue] == 0 ){
        
        titStr = [NSString stringWithFormat:@"商贷%@万",hut.bloan];
        
    }else{
        
        titStr = [NSString stringWithFormat:@"商贷%@万,公积金%@万",hut.bloan,hut.gloan];
    }
    
    cell.loanLabel.text = titStr;
    
    NSString* ratStr = @"";
    
    if ( [hut.brate floatValue]  == 0) {
        ratStr = [NSString stringWithFormat:@"%.2lf%@,%@%@",[hut.grate floatValue],@"%",hut.deadline,@"年"];
        
    }else if( [hut.grate floatValue] == 0){
        ratStr = [NSString stringWithFormat:@"%.2lf%@,%@%@",[hut.brate floatValue],@"%",hut.deadline,@"年"];
        
    }else{
        ratStr = [NSString stringWithFormat:@"%.2lf%@,%.2lf%@,%@%@",[hut.grate floatValue],@"%",[hut.brate floatValue],@"%",hut.deadline,@"年"];
    }
    
    cell.rateLabel.text = ratStr;
    
    if (self.loanType == BX) {
        cell.payLabel.text = hut.cmonth_bx;
        cell.totalLabel.text = hut.ctotal_bx;
        cell.unitLabel.text = @"元 / 月";
    }else if (self.loanType == BJ){
        cell.payLabel.text = hut.cmonth_bj;
        cell.totalLabel.text = hut.ctotal_bj;
        cell.unitLabel.text = @"元/首月";
    }
    
    return cell;
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
